"""
=================================================
@Author: Zenon
@Date: 2025-05-21
@Description：首页界面模块
==================================================
"""
from PySide6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel, 
                              QTextEdit, QGroupBox)
from PySide6.QtGui import QFont
from PySide6.QtCore import Qt

class HomePage(QWidget):
    """首页界面类"""
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        self.font = parent.font if parent else QFont("Arial", 12)
        self.init_ui()
        
    def init_ui(self):
        """初始化UI"""
        layout = QVBoxLayout(self)
        layout.setContentsMargins(15, 15, 15, 15)
        layout.setSpacing(20)

        # 统计信息区域
        stats_group = QGroupBox("系统统计")
        stats_group.setFont(self.font)
        stats_layout = QHBoxLayout(stats_group)
        stats_layout.setSpacing(20)

        # 模型统计
        model_stats = QWidget()
        model_stats_layout = QVBoxLayout(model_stats)
        model_stats_layout.setAlignment(Qt.AlignCenter)

        self.model_count_title = QLabel("模型数量")
        self.model_count_title.setFont(QFont("Arial", 14, QFont.Bold))
        self.model_count_title.setAlignment(Qt.AlignCenter)

        self.model_count_label = QLabel("0")
        self.model_count_label.setStyleSheet("""
            QLabel {
                border: 2px solid #007acc;
                border-radius: 50%;
                padding: 40px;
                background-color: #f0f8ff;
                color: #007acc;
                font-size: 24px;
                font-weight: bold;
            }
        """)
        self.model_count_label.setAlignment(Qt.AlignCenter)
        self.model_count_label.setMinimumSize(120, 120)

        model_stats_layout.addWidget(self.model_count_title)
        model_stats_layout.addWidget(self.model_count_label)

        # 检测统计
        detection_stats = QWidget()
        detection_stats_layout = QVBoxLayout(detection_stats)
        detection_stats_layout.setAlignment(Qt.AlignCenter)

        self.detection_count_title = QLabel("检测次数")
        self.detection_count_title.setFont(QFont("Arial", 14, QFont.Bold))
        self.detection_count_title.setAlignment(Qt.AlignCenter)

        self.detection_count_label = QLabel("0")
        self.detection_count_label.setStyleSheet("""
            QLabel {
                border: 2px solid #228B22;
                border-radius: 50%;
                padding: 40px;
                background-color: #F0FFF0;
                color: #228B22;
                font-size: 24px;
                font-weight: bold;
            }
        """)
        self.detection_count_label.setAlignment(Qt.AlignCenter)
        self.detection_count_label.setMinimumSize(120, 120)

        detection_stats_layout.addWidget(self.detection_count_title)
        detection_stats_layout.addWidget(self.detection_count_label)

        stats_layout.addWidget(model_stats)
        stats_layout.addWidget(detection_stats)

        # 系统日志区域
        log_group = QGroupBox("系统日志")
        log_group.setFont(self.font)
        log_layout = QVBoxLayout(log_group)

        self.log_text_edit = QTextEdit()
        self.log_text_edit.setPlaceholderText("系统日志将显示在这里...")
        self.log_text_edit.setReadOnly(True)
        self.log_text_edit.setFont(self.font)

        log_layout.addWidget(self.log_text_edit)

        layout.addWidget(stats_group)
        layout.addWidget(log_group)
        
    def update_model_count(self, count):
        """更新模型数量"""
        self.model_count_label.setText(str(count))
        
    def update_detection_count(self, count):
        """更新检测次数"""
        self.detection_count_label.setText(str(count))
        
    def get_log_text_edit(self):
        """获取日志文本编辑控件"""
        return self.log_text_edit